package com.dd.framework.jwt;

import com.dd.framework.security.SecurityUserDetails;
import com.dd.web.base.entity.view.AdminUserDetails;
import com.dd.web.base.service.UmsAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * 用户认证认证
 */
@Slf4j
@Component
public class JwtUserDetailsService implements UserDetailsService {

    @Autowired
    private UmsAdminService adminService;

    /**
     * 根据用户名查询用户
     * @param userName
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        log.debug("根据用户名{}查询用户",userName);
        AdminUserDetails adminUserDetails = adminService.loadUserByUsername(userName);
        SecurityUserDetails securityUserDetails = new SecurityUserDetails(adminUserDetails.getUmsAdmin().getUsername(),
                adminUserDetails.getAuthorityList());
        return securityUserDetails;
    }
}
